﻿using System;
using System.IO;
using System.Collections.Generic;

namespace SerialManager
{
    /// <summary>
    /// Trida uchovavajici seznam souboru souvisejici s jednou epizodou serialu
    /// </summary>
    class EpisodeFiles
    {
        internal readonly static string[] VideoExtensions = new string[] { "avi", "mpg", "wmv", "mkv", "mp4" };
        internal readonly static string[] SubtitleExtensions = new string[] { "srt", "sub", "txt" };

        private readonly List<FileInfo> _videos = new List<FileInfo>();
        private readonly List<FileInfo> _subtitles = new List<FileInfo>();

        /// <summary>
        /// Informace o epizode
        /// </summary>
        public EpisodeInfo Info { get; private set; }

        /// <summary>
        /// Seznam videosouboru
        /// </summary>
        public FileInfo[] Videos { get { return _videos.ToArray(); } }

        /// <summary>
        /// Seznam titulku
        /// </summary>
        public FileInfo[] Subtitles { get { return _subtitles.ToArray(); } }

        /// <summary>
        /// Konstruktor tridy
        /// </summary>
        /// <param name="info"></param>
        public EpisodeFiles(EpisodeInfo info)
        {
            Info = info;
        }

        private EpisodeFiles() { }

        /// <summary>
        /// Pridat soubor do seznamu
        /// Je automaticky zarazen podle pripony do spravne skupiny
        /// </summary>
        /// <param name="file"></param>
        public void Add( FileInfo file )
        {
            if( file.HasAnyExtension( VideoExtensions ) )
                _videos.Add( file );
            else if (file.HasAnyExtension( SubtitleExtensions ))
                _subtitles.Add( file );
        }

        /// <summary>
        /// Pripravit evidovane souboru k prejmenovani
        /// </summary>
        /// <param name="forMove">Kolekce, do ktere jsou zapsany soubory k presunu</param>
        public void PrepareMove( ref FileListBuilder.MoveResult result )
        {
            var path = Path.Combine( Core.SerialDirectory.FullName, "Season " + Info.Season );
            var name = String.Format( "{0} - {1:00}x{2:00}{3}",
                Core.SerialTitle,
                Info.Season, Info.Number,
                ( !String.IsNullOrEmpty( Info.Title ) ? " - " + Core.FixFileName( Info.Title ) : "" )
            );
            var fullpath = Path.Combine( path, name );

            foreach( var video in Videos )
                result.Add( video, fullpath );
            
            foreach( var sub in Subtitles )
                result.Add( sub, fullpath );
        }

        public override int GetHashCode()
        {
            return Info.GetHashCode();
        }

        public override bool Equals( object obj )
        {
            return Info.Equals( obj );
        }
    }
}
